﻿/*
 * Copyright (c) HiSilicon (Shanghai) Technologies Co., Ltd. 2023-2023. All rights reserved.
 */
#ifndef BURN_COMMON_H
#define BURN_COMMON_H
#include <string>
#include <sstream>
#include <iostream>
#include <vector>
#include <ctime>
#ifdef LINUX
#include <climits>
#include <unistd.h>
#include <cstring>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#else
#include <tchar.h>
#include <winsock.h>
#endif
#include <list>
#include <memory>
#include "../../Securec/securec.h"

extern "C"
{
    typedef void(*FPPrintInfo)(long long, const char*, const char*, const char*, int);
    typedef int(*IsCanceled)();
    typedef void(*FPSetProgress)(long long, int);
    extern FPPrintInfo g_printInfo;
    extern IsCanceled g_isCancelled;
    extern FPSetProgress g_setProgress;
}

namespace Common {
    enum CoreImages : int {
        CORE_IMAGES_BT = 0,         // BT
        CORE_IMAGES_PROTOCOL = 1,   // DSP
        CORE_IMAGES_APPS = 2,       // APP
        CORE_IMAGES_RECOVERY = 3,
        CORE_IMAGES_EXTERN0 = 4,    // DSP1
        CORE_INAGES_EXTERN1 = 5,
        CORE_IMAGES_EXTERN1_SSB = 6
    };
    // for CRC
    const unsigned short CRC_TABLE[256] = {
        0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7,
        0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
        0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6,
        0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de,
        0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485,
        0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d,
        0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4,
        0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc,
        0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823,
        0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b,
        0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12,
        0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a,
        0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41,
        0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49,
        0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70,
        0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78,
        0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f,
        0x1080, 0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067,
        0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e,
        0x02b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256,
        0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d,
        0x34e2, 0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
        0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c,
        0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634,
        0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab,
        0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3,
        0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a,
        0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92,
        0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9,
        0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1,
        0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8,
        0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0,
    };
#ifdef LINUX
#define  Sleep(x)  usleep((x) * (1000))
#define  MAX_PATH    PATH_MAX
#define _atoi64(val)     strtoll(val, nullptr, 10)
    const int INVALID_HANDLE_VALUE = -1;
    const int LOAD_OR_DUMP_IMAGE_PROGRESS = 1;
    using LPVOID = void *;
    const long MAXDWORD = 0xffffffff;
    const int TRUE = 1;
    const int FALSE = 0;
    const int UART_ENUM_MAX_NUM = 100;
#else
    const int LOAD_OR_DUMP_IMAGE_PROGRESS = 50;
#endif
#ifndef SAFE_FREE
#define SAFE_FREE(theAlloc) \
    do { \
        if (theAlloc) { \
            ::free(theAlloc); \
            theAlloc = nullptr; \
        } \
    } while (0)
#endif

#ifndef SAFE_DELETE
#define SAFE_DELETE(theObject) \
    do { \
        if (theObject) { \
            ::delete(theObject); \
            theObject = nullptr; \
        } \
    } while (0)
#endif // SAFE_DELETE
    // JTAG
    enum ChipGenAddr : int {
        GEN2,
        GEN8,
        GEN9,
        STATUS_GENREG,  // GEN31(CHIPK1), GEN42(only for CHIPW1)
        CHIPADDR,
        GEN36,
        GEN37,
        OUTDDR,
        INDDR,
        ACPU_ROM_START_FLAG,
        ACPU_ROM_START_ADDR_LOW,
        ACPU_AUTO_RST_REQ,
    };

    constexpr int PORT_IS_USE = 2;
    const int NACK = 0xA5;
    const int XACK = 0xCB;
    const int CONTINUE = 2;
    const int ACK_LEN = 6;
    const int RELEASE_CPU_TIME = 5;
    const int ESCAPE_CHAR = 0x5C;
    const int HEAD_FLAG = 0x735c;
    const int TAIL_FLAG = 0x655c;
    const int DEC = 10;
    const int HEX = 16;
    const int MAX_SIZE_BUFFER = 1024;
    const unsigned short USER_CANCEL = 0x100;
    const unsigned short OPENOCD_DEVICE_NOT_FOUND = 0x200;
    const unsigned short SERAIL_PORT_OPEN_FAILED = 0x300;
    const unsigned short SERAIL_PORT_NO_DATA = 0x301;
    const unsigned short SERAIL_PORT_BOOT_NOT_RUN = 0x302;
    const unsigned short BOOT_NOT_RUN = 0X303;
    const unsigned short INVALID_PARAMETER = 0x304;
    const unsigned short WRONG_CHIP_ID = 0x305;
    const unsigned short INVALID_BASIC = 0x306;
    const unsigned short DOWNLOAD_FILE_FAILED = 0x307;
    const unsigned short READ_REG_ERROR = 0x308;
    const unsigned short OPERATE_TIME_OUT = 0x309;
    const unsigned short INIT_OPENOCD_FAIL = 0x310;
    const unsigned short INIT_DDR_FAIL = 0x311;
    const unsigned short CRC32_VERIFY_TIMEOUT = 0x312;
    const unsigned short BOARD_POWER_OFF = 0x313;
    const int JTAG_OPEN = 2;
    const int JTAG_LOCK = 3;
    const int JTAG_CLOSE = 4;
    const int JTAG_NOT_FOUND = 5;
    const int JTAG_INIT_REG_ERROR = 6;
    const int JTAG_APB_AP_ERROR = 7;
    const int JTAG_SOFT_RESET_ERROR = 8;
    const int JTAG_NOT_INSTALL = 9;
    const int JTAG_IS_POWER_ON = 10;
    const int JTAG_ABNORMAL_AND_POWER_ON = 11;
    const int JTAG_NOT_CONNECTED = 12;
    const int JTAG_OPENOCD_CONFLIX = 13;
    const int JTAG_OTHER_ERROR = 50;
    const int JTAG_ERROR_NONE = -1;
    const int JTAG_LOAD_FILE_SUCCESS = 0;
    const int JTAG_LOAD_FILE_CANCEL = -1;
    const int JTAG_LOAD_FILE_FAIL = -2;
    const int JTAG_LOAD_FILE_POWER_OFF = -3;
    const int RECV_TIMEOUT = 100000;
    const int PROPORTIONS = 10;
    const int CMD_REGBIN_CRC32_VERIFY = 5;
    const int CMD_UBOOT_CRC32_VERIFY = 6;
    const int CMD_BOOT_PARAM_CRC_CHECK = 7;
    constexpr unsigned char SMART_HOME_WAIT_BURN_FLAG = 0x15;
    constexpr unsigned int WAIT_RUN_TIMEOUT = 3000;
    enum DumpStatus : int {
        RETURN_OK,
        CANCLE,
        RETURN_ERROR,
        RETURN_TIMEOUT,
    };

    const int XSTART = 0xFE;               // START FRAME
    const int XHEAD = 0xFE;               // HEAD FRAME
    const int XDATA = 0xDA;               // DATA FRAME
    const int XTAIL = 0xED;               // TAIL FRAME
    const int XCMD = 0xAB;               // CMD HEAD
    const int XKEY = 0xCD;               // CMD unsigned short
    const int ACK = 0xAA;               // ACK RESPONSE
    const int NAK = 0x55;               // NAK RESPONSE
    const int FAIL_STATUS = 0;
    const int SUCCESS_STATUS = 1;
    const int ONE_KBYTES = 0x400;
    const int ONE_MBYTES = 0x100000;
    const int ONE_GBYTES = 0x40000000;
    const int EMMC_BLOCK_SIZE = 512;
    const int FILE_RECEIVE = 512;
    const int BAUD_RATE_115200 = 115200;
    const int BAUD_RATE_500000 = 500000;
    const int BAUD_RATE_921600 = 921600;
    const int BAUD_RATE_1000000 = 1000000;
    const int BAUD_RATE_1500000 = 1500000;
    const int BAUD_RATE_2000000 = 2000000;
    const int I2C_CLOCK_SPEED = 100000;
    const int SPI_BAUD_RATE_PRESCALER = 32;
    const int CAN_BAUD_RATE = 125;
    const int XHDSHK = 0xBE;
    const int SHAKE_HAND_FRAME_LENGTH = 9;

    const int GROUPCTRL_STATUS_UNUSED = 1;
    const int BURNTHREAD_STATUS_CONNECTED = 2;
    const int BURNTHREAD_STATUS_STARTDOWNBOOT = 3;
    const int BURNTHREAD_STATUS_DOWNBOOT = 4;
    const int BURNTHREAD_STATUS_STARTBURNIMAGE = 5;
    const int BURNTHREAD_STATUS_BURNIMAGE = 6;
    const int BURNTHREAD_STATUS_FINISH = 7;
    const int BURNTHREAD_STATUS_ERROR = 8;
    const int MY_VID = 0x12D1;
    const int MY_PID = 0xD001;
    const int MAX_CHUNK_SIZE_EXT4 = ONE_MBYTES;
    const int EXT4_MAX_CHUNKS = (500 * ONE_MBYTES);
    const int GSL_LENGTH = 15 * 1024;
    const int GSL_START_POSITION = 64 * 1024;
    constexpr int MAX_READ_DATA_SIZE = 128 * 1024 * 1024; // 128Mb

    const int MSG_LEN = 128;
    const int USTART = 0xFA;
    const int UHEAD = 0xFE;
    const int UDATA = 0xDA;
    const unsigned int FRAME_TIMEOUT = 200;
    const unsigned int SERIAL_WRITE_TIMEOUT = 6000;
    const unsigned int MAX_DATA_FRAME_LEN = 1024;
    const unsigned int CMD_HEAD_FRAME_LENGTH = (5);
    const unsigned short COM_TIMEOUT = 0x11;
    const unsigned short COM_NACK = 0x12;
    const unsigned int TOTAL_TIMEOUT = 30000;
    const unsigned int HARDWARE_TIMEOUT = 30000;
    const unsigned int WAIT_BOOT_RUN_TIMEOUT = 30000;
    const int MAX_MSG_LEN = 512;
    const int CRC_LENGTH = 2;
    const int MAX_COM_NAME_LENGTH = 32;
    const int CMD_GET_BD_TYPE = 0;

    const int LOG_MAX_MAX_LEN = 1024;
    const int MAX_CMD_LENGTH = 128;
    const double UP_PROGRESS_BAR = 100.0;
    const int PERCENT_RATE = 100;
    constexpr int PERCENT_RATE_10 = 10;

    const int SECTOR_SIZE = 0x200;
    const int CURRENT_LOG_MAX_LEN = 0x100000;
    const int ACK_LENGTH = 2;
    const int HISTORY_LOG_MAX_LEN = 0x1000000;
    const int CHIP_PART_ONE = 1;
    const int CHIP_PART_TWO = 2;
    const int CHIP_PART_THREE = 3;
    const int CHIP_PART_FOUR = 4;
    const int CHIP_PART_FIVE = 5;
    const int CHIP_PART_SIX = 6;
    const int THREE_BYTE_ADDRESS_MODE = 3;
    const int FOUR_BYTE_ADDRESS_MODE = 4;
    const int SPI_SHAKE_HAND_FRAME_TIMEOUT = 3;

    const int PACKET_SIZE_32M = 32 * 0X100000;
    const unsigned int BOARD_FRAME_SEND_LENGTH = (14);
    const unsigned int BOARD_FRAME_REPLY_LENGTH = (11);
    const int MICROSECOND_UNIT = 1000;
    const int PARAM_CMD_MAX_LEN = 8;
    const int HEADER_LEN = 9;
    const int RECV_LEN = 4;
    const int DEVICE_ADDRESS = 1;
    const int I2C_WRITE_REGISTER_CMD = 15;
    const int I2C_READ_REGISTER_CMD = 16;
    const int SPI_WRITE_CMD = 17;
    const int SPI_READ_CMD = 18;
    const int UART_WRITE_REGISTER_CMD = 19;
    const int UART_READ_REGISTER_CMD = 20;
    const int CAN_WRITE_CMD = 25;
    const int CAN_READ_CMD = 26;
    const int TOGGLE_CAN_BAUD_RATE_CMD = 27;
    const int TOGGLE_I2C_BAUD_RATE_CMD = 28;
    const int TOGGLE_SPI_BAUD_RATE_CMD = 29;
    const int TOGGLE_UART_BAUD_RATE_CMD = 30;
    const int PER_PACKET_SIZE = 4096;
    const int TIME_DIFFERENCE = 1000000;
    constexpr int BOARD_POWERON_TIMEOUT = 30000;
    struct TouchFrameInfo {
        unsigned short cmd;
        unsigned char ack;
        unsigned char len;
        unsigned char param[PARAM_CMD_MAX_LEN];
        unsigned short cmdCrc;
        unsigned char reservse[2];
    };
    // BOOT数据发送单位长度
    const int MAX_DATA_FRAME_LEN_USB = 511;
    struct Partition {
        int flashType;
        int fileSystem;
        long long start;
        long long length;
        std::string selectFile;
    };

    enum CHIPTYPE : int {
        CHIPTYPEC1 = 0x10001,
        CHIPTYPEC2 = 0x10002,
        CHIPTYPEC3 = 0x10003,
        CHIPTYPEC4 = 0x10004,
        CHIPTYPEC5 = 0x10005,
        CHIPTYPEC6 = 0x10006,
        CHIPTYPEC7 = 0x10007,
        CHIPTYPEC8 = 0x10008,
        CHIPTYPEC9 = 0x10009,
        CHIPTYPEC10 = 0x1000A,
        CHIPVERS1 = 0x20001,
        CHIPVERW1 = 0x20002,
        CHIPVERK1 = 18,
        CHIPVERK2 = 19,
        CHIPS6,
        CHIPS7,
        CHIPS8,
        CHIPVER32 = 0x30002,
        CHIPVER33 = 0x30003,
        CHIPWS63 = 0x30004,
        CHIPLUOFU = 0x30005,
        CHIPXILING = 0x30006,
        CHIPEMEI = 0x30007,
        CHIPTIANGONG0 = 0x30008,
        CHIPTIANGONG1 = 0x30009,
        CHIPTIANGONG2 = 0x3000A,
        CHIPVER40 = 0x40000,
        CHIPVER41 = 0x40001,
        CHIPVER42 = 0x40002,
        CHIPVER43 = 0x40003,
        CHIPVER44 = 0x40004,
        CHIPVER45 = 0x40005,
        CHIPVER51 = 0x50001,
        CHIPVER52 = 0x50002,
        CHIPVER53 = 0x50003,
        CHIPVER61 = 0x60001,
        CHIPTYPE_BUTT
    };
    enum UsbTransformsType : int {
        USB_TO_USB = 0,
        USB_TO_UART,
        USB_TO_I2C,
        USB_TO_SPI,
        USB_TO_CAN,
        USB_TO_JTAG,
        USB_TO_SWD,
        USB_BUTT
    };
    const int NO_WRITE_TIMES = 2;
    const int IP_LEN = 16;
    const int UART_NAME_LEN = 32;
    const int USB_NAME_LEN = 64;

    union TRANSPORT_MODE_ATTR_U {
        struct {
            char uartName[UART_NAME_LEN];
            unsigned long baudRate;
            int stopBit;
            int parity;
        };
        struct {
            char ip[IP_LEN];
            unsigned short port;
        };
        struct {
            int vid;
            int pid;
            char usbDeviceName[USB_NAME_LEN];
        };
        struct {
            const char *targetCfg;
            int passwordMode;
            const char *dllPath;
            const char *interfaceCfg;
            int jtagFrequency;
            int netPort;
        };
    };
    // TcpTransport
    const int CONNECT_TIMEOUT = 5;
    const int STACK_LEN = 1024;
#ifdef WIN32
    const  int INVALID_SOCKET_VALUE = INVALID_SOCKET;
#else
    const  int INVALID_SOCKET_VALUE = -1;
#define  closesocket(sock) close(sock)
#endif

    enum FLASH_TYPE {
        FT_UNKNOWN = 0,
        FT_NAND = 1,
        FT_SPI = 2,
        FT_EMMC = 3,
        FT_UFS = 4,
    };

    enum FILE_SYSTEM {
        FS_UNKNOWM,
        FS_NONE,
        FS_YAFFS,
        FS_EXT3,
        FS_TAT32,
        FS_SQUASHFS,
        FS_UBIFS,
        FS_F2FS,
        FS_EROFS
    };
    enum INFO_TYPE : int {
        CONSOLE,
        DEBUG,
    };
    enum FileType : int {
        IMAGE_TYPE_LOADER,
        IMAGE_TYPE_NORMAL,
        IMAGE_TYPE_KV_NV,
        IMAGE_TYPE_EFUSE,
        IMAGE_TYPE_OTP,
        IMAGE_TYPE_FLASHBOOT,
        IMAGE_TYPE_FACTORY,
        IMAGE_TYPE_VERSION,
        IMAGE_TYPE_SECURITY_A,
        IMAGE_TYPE_SECURITY_B,
        IMAGE_TYPE_SECURITY_C,
        IMAGE_TYPE_PROTOCOL_A,
        IMAGE_TYPE_APPS_A,
        IMAGE_TYPE_RADIOCONFIG,
        IMAGE_TYPE_ROM,
        IMAGE_TYPE_EMMC,
        IMAGE_TYPE_DATABASE = 16, // as database bin 16
        IMAGE_TYPE_FLASHBOOT_3892,
        IMAGE_TYPE_APP,
        IMAGE_TYPE_APP_SIGN,
        IMAGE_TYPE_BT,
        IMAGE_TYPE_BT_SIGN,
        IMAGE_TYPE_DSP,
        IMAGE_TYPE_DSP_SIGN,
        IMAGE_TYPE_SSB_SHA,
        IMAGE_TYPE_SSB_SIGN,
        IMAGE_TYPE_DSP1,
        IMAGE_TYPE_DSP1_SIGN,
        IMAGE_TYPE_SMALL,
        IMAGE_TYPE_SMALL_SHA,
        IMAGE_TYPE_FLASH_BIN = 100,
        IMAGE_TYPE_EMMC_BIN = 101,
        IMAGE_TYPE_SHA = 102
    };
    struct FileHead {
        char name[32];                               /* image name to show, len 32 */
        unsigned int startIndex;                             /* image start index in whole file */
        unsigned int imageSize;                              /* image size */
        unsigned int burnAddr;                               /* burn addr in flash, like burn 0x200000 */
        unsigned int burnSize;                               /* burn size in burn cmd, like burn 0 200000,
                                                              * this size maybe bigger than imageSize */
        FileType type;                                       /* file type */
    };
    struct FwpkgHead {
        unsigned int flag;                                   /* a flag 0xefbeaddf */
        unsigned short crc;                                   /* from imageNum to head end */
        unsigned short imageNum;                              /* image count */
        unsigned int totalFileSize;                          /* file size */
        FileHead info[0];
    };

    struct FileHeadNew {
        unsigned char name[260];   // image name to show, len 260
        unsigned long startIndex; // image start index in whole file
        unsigned long imageSize;  // image size
        unsigned long burnAddr;   // burn addr in flash, like burn 0x200000
        unsigned long burnSize;   // burn size in burn cmd, like burn 0 200000
        FileType type;
    };

    struct FwpkgHeadNew {
        unsigned long flag;     // 0xefbeadd0
        unsigned short crc;      // from imageNum to head end
        unsigned short imageNum; // image count
        unsigned long totalFileSize;
        unsigned char name[260]; // image name to show, len 260
        FileHeadNew info[0];
    };
    enum TouchDataType : int {
        TP_DATA_HW_CAPBILITY = 0,
        TP_DATA_HW_VER_INFO,
        /* frame buf ptr, varies with a/b buf switch */
        TP_DATA_DB_HEAD,
        /* AP2IC */
        TP_DATA_AP_SETTING,
        /* IC2AP */
        TP_DATA_IC_RUNNING_STATUS,

        TP_DATA_CMD,
        TP_DATA_HAL_CMD,
        TP_DATA_EVENT,
        TP_DATA_DFX,
        TP_DATA_ALG_DEBUG_INFO,
        TP_DATA,
        TP_DATA_MAX,
    };
    enum class FlashPatitionType {
        FLASH_PATITION_ESBC,
        FLASH_PATITION_UBOOT,
        FLASH_PATITION_KERNEL,
        FLASH_PATITION_ROOTFS,
        FLASH_PATIOTION_FWK
    };

#ifdef LINUX
int fopen_s(FILE **fp, const char *file, const char *mode);
speed_t GetUartBaudRate(int baudRate);
#endif
void Print(const char *szInfo, const char *szDebugInfo, long long fd = 0, const char *devices = "");
void PrintInfo(long long fd, INFO_TYPE infoType, const char* format, ...);
void PrintSendRecv(int bSend, const unsigned char *pData, int nLen);
std::vector<std::string> Split(std::string strtem, char a);
clock_t GetTime();
FLASH_TYPE StringToFlashTypeEnum(const std::string &flashType);
std::string FlashTypeEnumToString(int flashType);
FILE_SYSTEM StringToFileSystemEnum(const std::string &fileSystem);
int GetFileContent(std::string strFilePath, unsigned char **fileContent, unsigned long &fileLen, long long fd = 0);
std::unique_ptr<unsigned char> GetFileData(std::string strFilePath, unsigned long &fileLen, long long fd = 0);
int GetFileLen(std::string path);
void OpenFile(FILE** ppFile, const char* szSrcPath, const char* mode);
unsigned char CalcCrc8(const unsigned char *buffer, unsigned int len);
unsigned short SopCrc16(const unsigned char *pData, unsigned int ulSize);
unsigned short CalcXModeCRC16(unsigned char *puchMsg, unsigned long usDataLen);
unsigned char CalculateCrc(const unsigned char *data, unsigned int length);
unsigned char CalcCheckSum8(const unsigned char *data, unsigned int size);
std::string FormatInfo(const char *format, ...);
bool IgnoreCaseCompare(const std::string &str1, const std::string &str2);
void CloseFile(FILE *&fp);
std::string Back2ForwardSlash(std::string strPath);
bool CheckPathisInValid(const char* path);
bool CheckPathisNetworkPath(const char* path);
bool CheckPathIsChinese(const char* path);
void PrintProgress(int& time, int progress);
bool Cancelled();
void InvertUintU32(unsigned int *srcBuf, unsigned long int len);
std::vector<unsigned char> GetDouble5CFrameData(unsigned char *pData, unsigned long dwLength);
void InitShakeHandCmd(unsigned char *shakeHandFrame, unsigned int frameLength, unsigned int baudRate);
bool CreateCmdProcess(HANDLE &readPipe, HANDLE writePipe, SECURITY_ATTRIBUTES &saAttr, std::string &portNumStr);
bool IsNotUsedPort(int portNum);
void DelayUs(double delayTime);
void ConvertingUintToArrays(unsigned char *arrays, int arraysLen, unsigned int value, int &index);
std::string GetFileName(const std::string &filePath);
};
#endif
